package com.yonyou.brigade.third_parties.third.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.third_parties.ctrip.bigcustomer.service.ICtripBigUserService;
import com.yonyou.brigade.third_parties.third.model.BriDeptAuthorization;
import com.yonyou.brigade.third_parties.third.dao.BriDeptAuthorizationMapper;
import com.yonyou.brigade.third_parties.third.service.IAuthUserUpdateObserver;
import com.yonyou.brigade.third_parties.third.service.IAuthUserUpdateObserverable;
import com.yonyou.brigade.third_parties.third.service.IBriDeptAuthorizationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangzhf@yonyou.com
 * @since 2018-08-16
 */
@Service
public class BriDeptAuthorizationServiceImpl extends ServiceImpl<BriDeptAuthorizationMapper, BriDeptAuthorization> implements IBriDeptAuthorizationService {

    @Autowired
    private BriDeptAuthorizationMapper briDeptAuthorizationMapper;
    @Autowired
    @Qualifier("authUserUpdateObserverable")
    private IAuthUserUpdateObserverable authUserUpdateObserverable;

    @Resource(name="ctripAuthUserUpdateObserver")
    private IAuthUserUpdateObserver ctripAuthUserUpdateObserver;

    @Autowired
    @Qualifier("ctripBigUserServiceImpl")
    private ICtripBigUserService ctripBCUserService;


    public Map findDeptAuthInfos(String org, int pageSize, int current, String condition, String setted)
    {
        Map result=new HashMap();
        List<BriDeptAuthorization> resultdepts=new ArrayList<>();
        int total=0;
        if(current<=0)
        {
            current=1;
        }
        int start=(current-1)*pageSize;
        int size=pageSize;
        try {
            Map params=new HashMap();
            params.put("start",start);
            params.put("size",size);
            params.put("orgpk",org);

            List<BriDeptAuthorization> resultDeptAuths=new ArrayList<>();
            total=briDeptAuthorizationMapper.selectDeptAuthInfoCount(params);
            resultDeptAuths=briDeptAuthorizationMapper.selectDeptAuthInfo(params);
            result.put("code","0");
            result.put("information",resultDeptAuths);
            JSONObject pagination=new JSONObject();
            pagination.put("total",total);
            pagination.put("pageSize",pageSize);
            pagination.put("current",current);
            result.put("pagination",pagination);
        }
        catch (Exception e)
        {
            result.put("code","1");
            result.put("information","操作失败");
            JSONObject pagination=new JSONObject();
            pagination.put("total",total);
            pagination.put("pageSize",pageSize);
            pagination.put("current",current);
            result.put("pagination",pagination);
        }

        return result;

    }

    public List<BriDeptAuthorization> selectDeptAuthInfoByOrg(String orgpk)
    {
        return  briDeptAuthorizationMapper.selectDeptAuthInfoByOrg(orgpk);
    }

    public Map updateDeptInfos(List<BriDeptAuthorization> deptAuths,String org)
    {
        Map result=new HashMap();
        if(deptAuths!=null&&deptAuths.size()>0)
        {
            authUserUpdateObserverable.removeObserver(ctripAuthUserUpdateObserver);
            authUserUpdateObserverable.registerObserver(ctripAuthUserUpdateObserver);
            List<String> deptpks=new ArrayList<>();
            for(int i=0;i<deptAuths.size();i++)
            {
                String curdeptId=deptAuths.get(i).getDeptPk();
                deptpks.add(curdeptId);
            }
            List<String> userids = ctripBCUserService.selectUserIdByDept(deptpks);
            Map tempResult= authUserUpdateObserverable.notifyObserver(userids, org, deptAuths.get(0).getUserId());
            if(!tempResult.get("code").equals("0"))
            {
                return tempResult;
            }
            for(int i=0;i<deptAuths.size();i++)
            {
                String curdeptId=deptAuths.get(i).getDeptPk();
                Map params=new HashMap();
                params.put("orgpk",org);
                params.put("deptpk",curdeptId);
                BriDeptAuthorization deptAuth=briDeptAuthorizationMapper.selectSingleDeptAuth(params);
                if(deptAuth!=null)
                {
                    briDeptAuthorizationMapper.updateDeptAuthInfo(deptAuths.get(i));
                }
                else
                {
                    briDeptAuthorizationMapper.insertDeptAuthInfo(deptAuths.get(i));
                }
            }
        }
        result.put("code","0");
        result.put("information","操作成功");
        return result;
    }



}
